//
//  JLWellcomeViewController.swift
//  新浪微博
//
//  Created by linlin on 15/11/25.
//  Copyright © 2015年 jilin. All rights reserved.
//

import UIKit

import SDWebImage

class JLWellcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        

        
        //获取用户头像
        if let avatar_large = JLUserAccount.loadAccount()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //没有头像就发送请求获取数据
        JLUserAccount.loadAccount()?.loadUserInfo()
        
        
    }
    
//    override func viewWillAppear(animated: Bool) {
//        super.viewWillAppear(animated)
//        iconViewAnimation()
//    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewAnimation()
    }
    
    
    //iconview约束属性
    private var iconViewMoveConstraints: NSLayoutConstraint?
    
    
    
    //iconView动画
    func iconViewAnimation() {
        iconViewMoveConstraints?.constant = -500
        UIView.animateWithDuration(2.0, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //更新layout --- 注意是跟新view的layout
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
              print("动画完毕")
                
              AppDelegate.swichViewRootController(true)
        }
    }
    
    
    
    
    
    
    
    
    //添加控件
    private func prepareUI() {
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(messageText)
        
        
        
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageText.translatesAutoresizingMaskIntoConstraints = false
        
        //autolayout
        //view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : backgroundImageView]))
       
        view.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[sv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["sv" : bgImageView]))
        
        view.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[sv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["sv" : bgImageView]))
        
        
        //iconView
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        iconViewMoveConstraints = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -150)
        view.addConstraint(iconViewMoveConstraints!)
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        
        //messageText
        view.addConstraint(NSLayoutConstraint(item: messageText, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))

        view.addConstraint(NSLayoutConstraint(item: messageText, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))

        
    }
    
    
    
    
    
    //懒加载
    private lazy var bgImageView: UIImageView = {
       var view = UIImageView()
        view.image = UIImage(named: "ad_background")
        return view
        
    }()

  
    private lazy var iconView: UIImageView = {
        var view = UIImageView()
        view.image = UIImage(named: "avatar_default_big")
        //设置圆角
        view.layer.cornerRadius = 40
        view.layer.masksToBounds = true
        
        return view
        
    }()
    
    private lazy var messageText: UILabel = {
        var lab = UILabel()
        lab.text = "欢迎回来"
        lab.textColor = UIColor.blackColor()
        lab.sizeToFit()
        return lab
    }()
    
}
